#include "stdafx.h"
#include "OgreUtils.h"


Ogre::SceneNode* createMeshNode(Ogre::SceneManager* sm, const std::string& filename)
{
	Ogre::Entity* entity = sm->createEntity(filename);
	Ogre::SceneNode* node = sm->getRootSceneNode()->createChildSceneNode();
	node->attachObject(entity);
	return node;
}

Ogre::SceneNode* createPointLightNode(Ogre::SceneManager* sm, const Ogre::Vector3& pos)
{
	Ogre::Light* light = sm->createLight();
	light->setType(Ogre::Light::LT_POINT);
	light->setPosition(pos);
	Ogre::SceneNode* node = sm->getRootSceneNode()->createChildSceneNode();
	node->attachObject(light);
	return node;
}

Ogre::SceneNode* createDirectionalLightNode(Ogre::SceneManager* sm, const Ogre::Vector3& dir)
{
	Ogre::Light* light = sm->createLight();
	light->setType(Ogre::Light::LT_DIRECTIONAL);
	light->setDirection(dir);
	Ogre::SceneNode* node = sm->getRootSceneNode()->createChildSceneNode();
	node->attachObject(light);
	return node;
}

Ogre::SceneNode* createPlainNode(Ogre::SceneManager* sm)
{
	Ogre::Plane plane(Ogre::Vector3::UNIT_Y, 1.f);
	Ogre::MeshManager::getSingleton().createPlane("ground", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 15000.f, 15000.f, 1, 1, true, 1, 20.f, 20.f, Ogre::Vector3::UNIT_Z);
	Ogre::Entity* entity = sm->createEntity("ground");
	entity->setMaterialName("Plain/Main");
	entity->setCastShadows(false);
	Ogre::SceneNode* node = sm->getRootSceneNode()->createChildSceneNode();
	node->attachObject(entity);
	return node;
}

void setSky(Ogre::SceneManager* sm,  Ogre::RenderWindow* window)
{
	Ogre::ColourValue fogColor(0.9f, 0.9f, 1.0f);
	sm->setFog(Ogre::FOG_LINEAR, fogColor, 0.0, 1000, 5000);
	window->getViewport(0)->setBackgroundColour(fogColor);
	Ogre::Plane plane;
	plane.d = 1000;
	plane.normal = Ogre::Vector3::NEGATIVE_UNIT_Y;
	sm->setSkyPlane(true, plane, "Sky/Cloudy", 500, 20, true, 0.5, 150, 150);
}

void setShadows(Ogre::SceneManager* sm)
{
	sm->setAmbientLight(Ogre::ColourValue(0.1f, 0.1f, 0.1f));
	sm->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_MODULATIVE);
	sm->setShadowTextureSize(2048);
	sm->setShadowFarDistance(250.f);
}

GameObject* createBox(World* world, const btVector3& size, const btTransform& trans, bool temp)
{
	btCollisionShape* shape = new btBoxShape(size);
	Ogre::SceneNode* node = createMeshNode(world->getSceneManager(), "box.mesh");
	node->setScale(size.x(), size.y(), size.z());
	float mass = size.x() * size.y() * size.z();
	GameObject* obj = world->addObject(node, shape, trans, mass, temp);	
	return obj;
}

GameObject* createBox(World* world, const btVector3& size, const btTransform& trans, float density)
{
	btCollisionShape* shape = new btBoxShape(size);
	Ogre::SceneNode* node = createMeshNode(world->getSceneManager(), "box.mesh");
	node->setScale(size.x(), size.y(), size.z());
	float mass = size.x() * size.y() * size.z()*density;
	GameObject* obj = world->addObject(node, shape, trans, mass, false);	
	return obj;
}

